using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using EcsSiteDSTableAdapters;

namespace SiteECSWeb
{
    [DataObject]
    public class EcsOfficeBLL
    {
        private EcsOfficTableAdapter _EcsOfficAdapter = null;

        protected EcsOfficTableAdapter EcsOfficAdapter
        {
            get
            {
                if (_EcsOfficAdapter == null)
                {
                    _EcsOfficAdapter = new EcsOfficTableAdapter();
                }
                return _EcsOfficAdapter;
            }

        }

        #region SelectEcsOffice
        //Get all Ecs Offices Data//
        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public EcsSiteDS.EcsOfficDataTable GetAllEcsOffice()
        {
            return EcsOfficAdapter.GetEcsOfficeDataRowRank();
        }

        public int GetTotalOfficesByInternalID(int EcsInternalID)
        {
            Nullable<int> totalNull =  (Nullable<int>)EcsOfficAdapter.
                TotalEcsOfficeByEcsInternalId(EcsInternalID);
            return totalNull.GetValueOrDefault();            
        }
        //Get Ecs Office Data By Office Id//
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.EcsOfficDataTable GetEcsOfficeByOfficeID(int Office_id)
        {
            if (Office_id <= 0)
            {
                return EcsOfficAdapter.GetEcsOfficeDataRowRank();
            }
            else
            {
                return EcsOfficAdapter.GetEcsOfficeDataByOfficeId(Office_id);
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.EcsOfficDataTable GetEcsOfficeByCityID(int CityID)
        {
            return EcsOfficAdapter.GetEcsOfficeDataByCityID(CityID);
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.EcsOfficDataTable GetEcsOfficeByCityName(string CityName)
        {
            return EcsOfficAdapter.GetEcsOfficeDataByCityName(CityName);
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.EcsOfficDataTable GetEcsOfficeByEmail(string OffEmail)
        {
            return EcsOfficAdapter.GetEcsOfficeDataByEmail(OffEmail);
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.EcsOfficDataTable 
            GetEcsOfficeByInternalID(int InternalID)
        {
            return EcsOfficAdapter.GetEcsOfficeDataByEcsInternalId(InternalID);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.EcsOfficDataTable GetEcsOfficesCities()
        {
            return EcsOfficAdapter.GetEcsOfficesCitysData();
        }
      
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public List<OfficeInfo> GetEcsHeadOfficesCitiesAdvanced()
        {
            return GetEcsHeadOfficesCitiesAdvanced(-1);
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public List<OfficeInfo> GetEcsHeadOfficesCitiesAdvanced(int InternalID)
        {
            return GetEcsHeadOfficesCitiesAdvanced(InternalID,null);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public List<OfficeInfo> GetEcsHeadOfficesCitiesAdvanced(int InternalID, string CityNameToFind)
        {
            List<OfficeInfo> OfficeList = new List<OfficeInfo>();
            
            if (!string.IsNullOrEmpty(CityNameToFind))
            {
                int len = CityNameToFind == null ? 0 : CityNameToFind.Length;
                if (InternalID <= 0 )
                {
                    EcsSiteDS.EcsOfficDataTable OfficeTable = 
                        EcsOfficAdapter.GetEcsOfficeHeadCitysData();
                    foreach (EcsSiteDS.EcsOfficRow OfficeRow in OfficeTable)
                    {
                        OfficeInfo _offcieInfo =
                        new OfficeInfo(OfficeRow.Office_id, 
                            OfficeRow.CityNameAr, OfficeRow.City_nmen,
                            OfficeRow.Office_phne1, OfficeRow.Office_fax1,
                            OfficeRow.Office_email1, OfficeRow.CountyFlagSmallIcon,
                            OfficeRow.Ecsemp_nmar, OfficeRow.Ecsemp_mobilOff,
                            OfficeRow.Ecs_InternalOffice_ID, OfficeRow.EcsInternalName);

                        int compareLength = Math.Min(len, OfficeRow.CityNameAr.Length);

                        if (OfficeRow.CityNameAr.Substring(0, compareLength).
                            ToLower().Equals(CityNameToFind.Substring(0, compareLength).ToLower())
                            && OfficeRow.CityNameAr.Length >= len)
                        {
                            OfficeList.Add(_offcieInfo);
                        }
                    }
                }
                else
                {
                    EcsSiteDS.EcsOfficDataTable OfficeTable =
                        EcsOfficAdapter.GetEcsOfficeDataByEcsInternalId(InternalID);

                    foreach (EcsSiteDS.EcsOfficRow OfficeRow in OfficeTable)
                    {
                        OfficeInfo _offcieInfo =
                            new OfficeInfo(OfficeRow.Office_id, OfficeRow.CityNameAr, OfficeRow.City_nmen,
                            OfficeRow.Office_phne1, OfficeRow.Office_fax1,
                            OfficeRow.Office_email1, OfficeRow.CountyFlagSmallIcon,
                            OfficeRow.Ecsemp_nmar, OfficeRow.Ecsemp_mobilOff,
                            OfficeRow.Ecs_InternalOffice_ID, OfficeRow.EcsInternalName);
                        int compareLength = Math.Min(len, OfficeRow.CityNameAr.Length);

                        if (OfficeRow.CityNameAr.Substring(0, compareLength).
                            ToLower().Equals(CityNameToFind.Substring(0, compareLength).ToLower())
                            && OfficeRow.CityNameAr.Length >= len)
                        {
                            OfficeList.Add(_offcieInfo);
                        }
                    }
                }
            }
            else
            {
                if (InternalID <= 0)
                {
                    EcsSiteDS.EcsOfficDataTable OfficeTable = 
                        EcsOfficAdapter.GetEcsOfficeHeadCitysData();
                    foreach (EcsSiteDS.EcsOfficRow OfficeRow in OfficeTable)
                    {
                        OfficeInfo _offcieInfo =
                        new OfficeInfo(OfficeRow.Office_id, 
                            OfficeRow.CityNameAr, OfficeRow.City_nmen,
                            OfficeRow.Office_phne1, OfficeRow.Office_fax1,
                            OfficeRow.Office_email1, OfficeRow.CountyFlagSmallIcon,
                            OfficeRow.Ecsemp_nmar, OfficeRow.Ecsemp_mobilOff,
                            OfficeRow.Ecs_InternalOffice_ID, OfficeRow.EcsInternalName);
                        OfficeList.Add(_offcieInfo);
                    }
                }
                else
                {
                    EcsSiteDS.EcsOfficDataTable OfficeTable =
                   EcsOfficAdapter.GetEcsOfficeDataByEcsInternalId(InternalID);
                    foreach (EcsSiteDS.EcsOfficRow OfficeRow in OfficeTable)
                    {
                        OfficeInfo _offcieInfo =
                            new OfficeInfo(OfficeRow.Office_id, OfficeRow.CityNameAr, OfficeRow.City_nmen,
                            OfficeRow.Office_phne1, OfficeRow.Office_fax1,
                            OfficeRow.Office_email1, OfficeRow.CountyFlagSmallIcon,
                            OfficeRow.Ecsemp_nmar, OfficeRow.Ecsemp_mobilOff,
                            OfficeRow.Ecs_InternalOffice_ID, OfficeRow.EcsInternalName);
                        OfficeList.Add(_offcieInfo);
                    }
                }
            }
          
            return OfficeList;
        }


     
       
        #endregion


        #region InsertEcsOffice
        // insert All Column into EcsOffice Table//
        [DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
        public bool AddEcsOffice(int Ecs_InternalOffice_ID, int City_id, string Office_addrs, string Office_phne1,
            string Office_phne2, string Office_fax1, string Office_fax2,
            string Office_email1, string Office_email2, string Office_pscod, 
            string Office_website, string Office_memo,
            string Office_WrkingHours, string Office_WrkDayOff,
            string Office_latmap, string Office_longmap)
        {

            EcsSiteDS.EcsOfficDataTable EcsOfficeTable = new EcsSiteDS.EcsOfficDataTable();
            EcsSiteDS.EcsOfficRow EcsOfficeRow = EcsOfficeTable.NewEcsOfficRow();

            if (string.IsNullOrEmpty(Office_addrs)) EcsOfficeRow.SetOffice_addrsNull(); else EcsOfficeRow.Office_addrs = Office_addrs;
            if (string.IsNullOrEmpty(Office_phne2)) EcsOfficeRow.SetOffice_phne2Null(); else EcsOfficeRow.Office_phne2 = Office_phne2;
            if (string.IsNullOrEmpty(Office_fax2)) EcsOfficeRow.SetOffice_fax2Null(); else EcsOfficeRow.Office_fax2 = Office_fax2;
            if (string.IsNullOrEmpty(Office_email2)) EcsOfficeRow.SetOffice_email2Null(); else EcsOfficeRow.Office_email2 = Office_email2;
            if (string.IsNullOrEmpty(Office_pscod)) EcsOfficeRow.SetOffice_pscodNull(); else EcsOfficeRow.Office_pscod = Office_pscod;
            if (string.IsNullOrEmpty(Office_website)) EcsOfficeRow.SetOffice_websiteNull(); else EcsOfficeRow.Office_website = Office_website;
            if (string.IsNullOrEmpty(Office_memo)) EcsOfficeRow.SetOffice_memoNull(); else EcsOfficeRow.Office_memo = Office_memo;
            if (string.IsNullOrEmpty(Office_WrkingHours)) EcsOfficeRow.SetOffice_WrkingHoursNull(); else EcsOfficeRow.Office_WrkingHours = Office_WrkingHours;
            if (string.IsNullOrEmpty(Office_WrkDayOff)) EcsOfficeRow.SetOffice_WrkDayOffNull(); else EcsOfficeRow.Office_WrkDayOff = Office_WrkDayOff;
            if (string.IsNullOrEmpty(Office_latmap)) EcsOfficeRow.SetOffice_latmapNull(); else EcsOfficeRow.Office_longmap = double.Parse(Office_latmap);
            if (string.IsNullOrEmpty(Office_longmap)) EcsOfficeRow.SetOffice_longmapNull(); else EcsOfficeRow.Office_longmap = double.Parse(Office_longmap);


            EcsOfficeRow.Ecs_InternalOffice_ID = Ecs_InternalOffice_ID;
            EcsOfficeRow.City_id = City_id;
            EcsOfficeRow.Office_phne1 =Office_phne1;
            EcsOfficeRow.Office_fax1 =Office_fax1;
            EcsOfficeRow.Office_email1 = Office_email1;
            
            int rowsAffected = EcsOfficAdapter.Update(EcsOfficeTable);
            return rowsAffected == 1;
        }

        #endregion

        #region updateEcsOffice
        [DataObjectMethodAttribute(DataObjectMethodType.Update, true)]
        public bool updateEcsOffice(int Ecs_InternalOffice_ID, int City_id, 
            string Office_addrs, string Office_phne1,
            string Office_phne2, string Office_fax1, string Office_fax2,
            string Office_email1, string Office_email2, string Office_pscod, 
            string Office_website, string Office_memo, 
            string Office_WrkingHours, string Office_WrkDayOff,
            string Office_latmap, string Office_longmap,
            int Office_id)
        {
            EcsSiteDS.EcsOfficDataTable EcsOfficeTable = EcsOfficAdapter.GetEcsOfficeDataByOfficeId(Office_id);
            if (EcsOfficeTable.Count == 0)
            {
                return false;
            }
            EcsSiteDS.EcsOfficRow EcsOfficeRow = EcsOfficeTable[0];

            if (string.IsNullOrEmpty(Office_addrs )) EcsOfficeRow.SetOffice_addrsNull(); else EcsOfficeRow.Office_addrs = Office_addrs;
            if (string.IsNullOrEmpty(Office_phne2 )) EcsOfficeRow.SetOffice_phne2Null(); else EcsOfficeRow.Office_phne2 = Office_phne2;
            if (string.IsNullOrEmpty(Office_fax2 )) EcsOfficeRow.SetOffice_fax2Null(); else EcsOfficeRow.Office_fax2 = Office_fax2;
            if (string.IsNullOrEmpty(Office_email2 )) EcsOfficeRow.SetOffice_email2Null(); else EcsOfficeRow.Office_email2 = Office_email2;
            if (string.IsNullOrEmpty(Office_pscod )) EcsOfficeRow.SetOffice_pscodNull(); else EcsOfficeRow.Office_pscod = Office_pscod;
            if (string.IsNullOrEmpty(Office_website )) EcsOfficeRow.SetOffice_websiteNull(); else EcsOfficeRow.Office_website = Office_website;
            if (string.IsNullOrEmpty(Office_memo )) EcsOfficeRow.SetOffice_memoNull(); else EcsOfficeRow.Office_memo = Office_memo;
            if (string.IsNullOrEmpty(Office_WrkingHours)) EcsOfficeRow.SetOffice_WrkingHoursNull(); else EcsOfficeRow.Office_WrkingHours = Office_WrkingHours;
            if (string.IsNullOrEmpty(Office_WrkDayOff)) EcsOfficeRow.SetOffice_WrkDayOffNull(); else EcsOfficeRow.Office_WrkDayOff = Office_WrkDayOff;
            if (string.IsNullOrEmpty(Office_latmap)) EcsOfficeRow.SetOffice_latmapNull(); else EcsOfficeRow.Office_longmap= double.Parse(Office_latmap);
            if (string.IsNullOrEmpty(Office_longmap)) EcsOfficeRow.SetOffice_longmapNull(); else EcsOfficeRow.Office_longmap = double.Parse(Office_longmap);


            EcsOfficeRow.Ecs_InternalOffice_ID = Ecs_InternalOffice_ID;
            EcsOfficeRow.City_id = City_id;
            EcsOfficeRow.Office_phne1 = Office_phne1;
            EcsOfficeRow.Office_fax1 = Office_fax1;
            EcsOfficeRow.Office_email1 = Office_email1;
            EcsOfficeRow.Office_id = Office_id;            
            int rowsAffected = EcsOfficAdapter.Update(EcsOfficeRow);
            return rowsAffected == 1;

        }
        //[DataObjectMethodAttribute(DataObjectMethodType.Update, false)]
        //public bool updateEcsOffice(int Ecs_InternalOffice_ID, int City_id, string Office_addrs, string Office_phne1,
        //    string Office_phne2, string Office_fax1, string Office_fax2,
        //    string Office_email1, string Office_email2, string Office_pscod, string Office_website, string Office_memo, int Office_id)
        //{
        //    EcsSiteDS.EcsOfficDataTable EcsOfficeTable = EcsOfficAdapter.GetEcsOfficeDataByOfficeId(Office_id);
        //    if (EcsOfficeTable.Count == 0)
        //    {
        //        return false;
        //    }
        //    EcsSiteDS.EcsOfficRow EcsOfficeRow = EcsOfficeTable[0];

        //    if (Office_addrs == null) EcsOfficeRow.SetOffice_addrsNull(); else EcsOfficeRow.Office_addrs = Office_addrs;
        //    if (Office_phne2 == null) EcsOfficeRow.SetOffice_phne2Null(); else EcsOfficeRow.Office_phne2 = Office_phne2;
        //    if (Office_fax2 == null) EcsOfficeRow.SetOffice_fax2Null(); else EcsOfficeRow.Office_fax2 = Office_fax2;
        //    if (Office_email2 == null) EcsOfficeRow.SetOffice_email2Null(); else EcsOfficeRow.Office_email2 = Office_email2;
        //    if (Office_pscod == null) EcsOfficeRow.SetOffice_pscodNull(); else EcsOfficeRow.Office_pscod = Office_pscod;
        //    if (Office_website == null) EcsOfficeRow.SetOffice_websiteNull(); else EcsOfficeRow.Office_website = Office_website;
        //    if (Office_memo == null) EcsOfficeRow.SetOffice_memoNull(); else EcsOfficeRow.Office_memo = Office_memo;


        //    EcsOfficeRow.Ecs_InternalOffice_ID = Ecs_InternalOffice_ID;
        //    EcsOfficeRow.City_id = City_id;
        //    EcsOfficeRow.Office_phne1 = Office_phne1;
        //    EcsOfficeRow.Office_fax1 = Office_fax1;
        //    EcsOfficeRow.Office_email1 = Office_email1;
        //    EcsOfficeRow.Office_id = Office_id;

        //    int rowsAffected = EcsOfficAdapter.Update(EcsOfficeRow);
        //    return rowsAffected == 1;

        //}

        #endregion


        #region DeleteEcsOffice
        [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
        public bool DeleteEcsOffice(int Office_id)
        {
            int rowsAffected = EcsOfficAdapter.Delete(Office_id);
            return rowsAffected == 1;
        }

        #endregion

    }
}

